package com.yanqu.road.server.cmd.activity.gold;

import com.yanqu.road.entity.activity.ActivityDisperseGoldInfo;
import com.yanqu.road.entity.activity.ActivityDisperseGoldRecord;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.ActivityDisperseGoldProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityDisperseGoldModule;
import com.yanqu.road.server.gameplayer.module.activity.ActivityTrystModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.HolidayActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.server.manger.activity.throwingPot.ThrowingPotMgr;
import com.yanqu.road.server.manger.activity.treasure.TreasureActivityMgr;
import com.yanqu.road.server.manger.activity.winterLove.WinterLoveMgr;
import com.yanqu.road.server.manger.activity.yearMaster.YearMasterMgr;
import com.yanqu.road.server.manger.player.ActivityDisperseGoldMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;


@Cmd(code = Protocol.S_ACTIVITY_DISPERSE_GOLD_GET_REWARD , desc = "领取活动送财奖励")
public class GetActivityGoldRewardCmd extends AbstractCommand {

    private long disperseId;
    private String reward;
    private Property property;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityDisperseGoldProto.ActivityDisperseGoldGetRewardReqMsg reqMsg = ActivityDisperseGoldProto.ActivityDisperseGoldGetRewardReqMsg.parseFrom(packet.getBytes());
        disperseId = reqMsg.getDisperseId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityDisperseGoldInfo disperseGoldInfo = ActivityDisperseGoldMgr.getDisperseGoldInfo(disperseId);
        if (disperseGoldInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_DISPERSE_GOLD_NOT_FIND;
            return;
        }
        if (ActivityDisperseGoldMgr.isDisperseGoldEnd(disperseGoldInfo)) {
            ret = GameErrorCode.E_ACTIVITY_DISPERSE_GOLD_TIME_OVER;
            return;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(disperseGoldInfo.getActivityId());
        if(activityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        ActivityDisperseGoldRecord record = player.getModule(ActivityDisperseGoldModule.class).getActivityDisperseGoldRecord(disperseId);
        if(null != record){
            ret = GameErrorCode.E_ACTIVITY_DISPERSE_GOLD_HAD_GET_REWARD;
            return;
        }
        if(activityInfo.getType() == eActivityType.CrossHolidayActivity.getValue()) {
            //灵猫活动领奖
            property = HolidayActivityMgr.getDisperseGoldReward(disperseGoldInfo, activityInfo, player.getUserInfo().getEarnSpeed());

        }else if(activityInfo.getType() == eActivityType.ThrowingPotActivity.getValue()){
            //投壶飞箭送礼领奖
            property = ThrowingPotMgr.getDisperseGoldReward(player.getUserInfo().getEarnSpeed());
        }else if(activityInfo.getType() == eActivityType.YearMasterActivity.getValue()){
            //瑞兽活动领奖
            property = YearMasterMgr.getDisperseGoldReward(player.getUserInfo().getEarnSpeed());

        }else if(activityInfo.getType() == eActivityType.TreasureActivity.getValue()){
            //灵狐活动送奖
            property = TreasureActivityMgr.getDisperseGoldReward(activityInfo, player.getUserInfo().getEarnSpeed());

        } else if (activityInfo.getType() == eActivityType.TrystActivity.getValue()) {
            property = player.getModule(ActivityTrystModule.class).getDisperseGoldReward(activityInfo, player.getUserInfo().getEarnSpeed());
        } else if (activityInfo.getType() == eActivityType.CrossHuaShiGangActivity.getValue()) {
            property = HuaShiGangMgr.getDisperseGoldReward(player.getUserInfo().getEarnSpeed());
        } else if (activityInfo.getType() ==eActivityType.WinterLoveActivity.getValue()){
            //冬日恋歌领奖
            property = WinterLoveMgr.getDisperseReward(player.getUserInfo().getEarnSpeed());
        } else if(activityInfo.getType() == eActivityType.ShuiHuYeZiCardActivity.getValue()) {
            // 水浒叶子牌
            property = ShuiHuYeZiCardMgr.getDisperseGoldReward(player);
        } else if (activityInfo.getType() == eActivityType.Tomb.getValue()){
            property = TombMgr.getDisperseGoldReward(player.getUserInfo().getEarnSpeed());
        } else {
            //其他活动
        }

        // 添加道具
        if (property != null) {
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.Activity, eLogMoneyType.ActivityDisperseGoldReward);
            reward = PropertyHelper.parsePropertyToString(property);
        }

        if(!StringUtils.isNullOrEmpty(reward)){
            //更新总领奖情况
            ActivityDisperseGoldMgr.summaryReward(disperseGoldInfo, PropertyHelper.parseStringToProperty(reward));
            player.getModule(ActivityDisperseGoldModule.class).setRecord(disperseId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        ActivityDisperseGoldProto.ActivityDisperseGoldGetRewardRespMsg.Builder respMsg = ActivityDisperseGoldProto.ActivityDisperseGoldGetRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            respMsg.setReward(reward);
        }
        player.sendPacket(Protocol.U_ACTIVITY_DISPERSE_GOLD_GET_REWARD, respMsg);
    }
}
